<?php

class PieChartLibrary {
    
    public static function loadPieChart($apointmentsByType){
        $apointmentTypes = array("Reservada","Inconclusa","Cancelada", "Anulada", "Confirmada", "En Atención", "Atendida", "Calificada");
        
        $isEmpty = FALSE;
        
        for($i = 0; $i < 8; $i++){
            $pieces[]="['$apointmentTypes[$i]',$apointmentsByType[$i]]";
            // comprobando si se encontró citas
            if($apointmentsByType[$i] == 0){
              $isEmpty = TRUE;  
            }else{
                $isEmpty = FALSE;
            }
        }
        
        $alertMessage = "";
        
        if ($isEmpty) {
            $alertMessage = 'alert("No se ha encontrado citas");';
        }
        
        
        $array= implode(",", $pieces);
        
        $headScript="
            google.load('visualization', '1', {packages:['corechart']});
              google.setOnLoadCallback(drawChart);
            function drawChart() {
                var data = google.visualization.arrayToDataTable([
                    ['Citas', 'Cantidades'],
                    $array
                ]);

                var options = {'title':'Porcentaje y cantidad de citas',
                                is3D: true};

                var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
                chart.draw(data, options);
            }".$alertMessage;
        return $headScript;
    }
    
    public static function joinHeaderText($apTypes){
        $string = "";
        $comma = ",";
        for($i=0;$i<count($apTypes);$i++){
            if ($i<1) {
                $string = $string."'".$apTypes[$i]."'";
            }else{
                $string = $string.$comma."'".$apTypes[$i]."'";
            }
        }
        return $string;
    }
    
    public static function joinBodyText($specialties){
        $last = "";
        $string = "";
        $comma = ",";
        
        for($i=0;$i<count($specialties);$i++){
            $specialty = $specialties[$i];
            
            if ($i<1) {
                
                $string2 = "";
                $comma2 = ",";
                
                for($k=0;$k<count($specialty);$k++){
                    if($k<1){
                        $string2 = $string2."['".$specialty[$k]."'";
                        
                        
                    }else{
                        
                        if($k == count($specialty)-1){
                            $string = $string2.$comma2.$specialty[$k]."]";
                            break;
                        }else{
                            $string2 = $string2.$comma2.$specialty[$k];
                            
                        }
                    }
                    // $string = $string."['".$specialty[0]."',".$specialty[1].",".$specialty[2].",".$specialty[3]."]";
                }
                
                
            }else{
                
                
                $string3 = "";
                $comma3 = ",";
                
                for($j=0;$j<count($specialty);$j++){
                    if($j<1){
                        $string3 = $string3."['".$specialty[$j]."'";
                    }else{
                        if($j == count($specialty)-1){
                            $string = $string.$comma.$string3.$comma3.$specialty[$j]."]";
                            
                        }else{
                            $string3 = $string3.$comma3.$specialty[$j];
                        }
                    }
                    // $string = $string."['".$specialty[0]."',".$specialty[1].",".$specialty[2].",".$specialty[3]."]";
                }
                //$string = $string.$comma."['".$specialty[0]."',".$specialty[1].",".$specialty[2].",".$specialty[3]."]";
            }
        }
          
        return $string;
    }

    
    public static function loadBarChart($apTypes, $specialties){
        //$apTypes = array("Reservada","Inconclusa","Cancelada", "Anulada", "Confirmada", "En Atención", "Atendida", "Calificada");
        
        
        $headerString = self::joinHeaderText($apTypes);
        $bodyString = self::joinBodyText($specialties);
        //echo $bodyString;
        
        $barScript ="
            google.load('visualization', '1', {packages:['corechart']});
            google.setOnLoadCallback(drawBarChart);
            function drawBarChart() {
            var data = google.visualization.arrayToDataTable([
        
          ['Especialidades', "." $headerString "." ],".$bodyString."
          

        ]);

        var options = {
          title: 'Total de Citas',
          hAxis: {title: 'Especialidades', titleTextStyle: {color: 'red'}},
          vAxis: {title: 'Citas', titleTextStyle: {color: 'blue'}}
        };

        var barChart = new google.visualization.ColumnChart(document.getElementById('barChartDiv'));
        barChart.draw(data, options);
      }
            ";
        
        return $barScript;
    }
    
}

?>
